#/bin/python
#coding:utf8
'''
Created on 2011-5-8

@author: mark
'''

import config
from model import ptype
from model import problem
from model import answer
from model import comment
from util import auth
import threading
import time
import web

tp = config.template_index


list_pro = []
list_comments = []

class get_index_data(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global list_pro
        global list_ans
        while True:
            list_pro = problem.getNewProblem(6)
            list_comments = comment.getNewComment(2)
            print "thread start success!"
            time.sleep(5 * 60)

class initIndex:
    def GET(self):
        global monthHotProblems
        global weekHotProblems
        
        try:
            icookie = web.cookies()['ixuan_session']
        except Exception:
            icookie = None
            
        if icookie:
            user = auth.validatecookie(icookie)
            if user:
                web.ctx.session['current_user'] = user
        
        list_type = ptype.getTypes()
        current_user = web.ctx.session['current_user']
        
        monthHotProblems = problem._getMonthHotProblems(10)
        weekHotProblems = problem._getWeekHotProblems(10)
        
        context = {"types":list_type, \
                   "list_pro":list_pro, \
                   "list_comments":list_comments, \
                   "user":current_user,\
                   "monthHotProblems":monthHotProblems,\
                   "weekHotProblems":weekHotProblems
                   }
        return tp.index(context)
